/*
 * BLLib.Configuration.(TypeConverters)
 * Andy Tidball
 * 
 * Project: Black Lightning General Library
 * Copyright: GNU General Public License
 */

using System;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Windows.Forms;

namespace BLLib.Configuration {
	/// <summary>
	/// Converts a string value back and forth between from a simple hex encoding of the string.
	/// </summary>
	public class PasswordTypeConverter : TypeConverter {
		/// <summary>
		/// Checks if the converter can convert values from a given type to its type.
		/// </summary>
		/// <param name="Context">Contextual information about the conversion.</param>
		/// <param name="SourceType">The type to check for a conversion from.</param>
		/// <returns>True if the converter can convert a value from the given type to its type.</returns>
		public override bool CanConvertFrom(ITypeDescriptorContext Context, Type SourceType) {
			if (SourceType == typeof(string)) {
				return true;
			} else {
				return false;
			}
		}

		/// <summary>
		/// Checks if the converter can convert values from its type to a given type.
		/// </summary>
		/// <param name="Context">Contextual information about the conversion.</param>
		/// <param name="DestinationType">The type to check for a conversion to.</param>
		/// <returns>True if the converter can convert a value from its type to the given type.</returns>
		public override bool CanConvertTo(ITypeDescriptorContext Context, Type DestinationType) {
			if (DestinationType == typeof(string)) {
				return true;
			} else {
				return false;
			}
		}

		/// <summary>
		/// Converts a value from the given type to the converter's type.
		/// </summary>
		/// <param name="Context">Contextual information about the conversion.</param>
		/// <param name="Culture">The culture that the conversion is happening in.</param>
		/// <param name="Value">The value to convert.</param>
		/// <returns>The given value after having been converted.</returns>
		public override object ConvertFrom(ITypeDescriptorContext Context, CultureInfo Culture, object Value) {
			try {
				string Input = Value as string;
				byte[] Output = new byte[Input.Length/2];
				for (int i=0; i<Input.Length; i+=2) {
					string HexString = Input.Substring(i, 2);
					Output[i/2] = byte.Parse(HexString, NumberStyles.HexNumber);
				}
				return Encoding.ASCII.GetString(Output);
			} catch (Exception) {
				// they entered a password manually
				// return whatever they entered and it will get converted back to an encoded value when displayed
				return Value;
			}
		}

		/// <summary>
		/// Converts a value from the converter's type to the given type.
		/// </summary>
		/// <param name="Context">Contextual information about the conversion.</param>
		/// <param name="Culture">The culture that the conversion is happening in.</param>
		/// <param name="Value">The value to convert.</param>
		/// <param name="DestinationType">The type to convert the value to.</param>
		/// <returns>The given value after having been converted.</returns>
		public override object ConvertTo(ITypeDescriptorContext Context, CultureInfo Culture, object Value, Type DestinationType) {
			byte[] Input = Encoding.ASCII.GetBytes(Value as string);
			string Output = "";
			foreach (byte B in Input) {
				Output += B.ToString("x");
			}
			return Output;
		}
	}
}